package com.guardchina.framework.mybatis.aspect;

import com.guardchina.framework.mybatis.annotation.UseDataSource;
import com.guardchina.framework.mybatis.datasource.RoutingDataSourceContextHolder;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

/**
 * 描述：动态数据源切面
 *
 * @author lwb
 * @date 2019/3/22 13:17
 */
@Aspect
@Order(0)
public class RoutingDataSourceAspect {

    @Before("@annotation(ds)")
    public void switchDataSource(UseDataSource ds){
        RoutingDataSourceContextHolder.setDataSourceType(ds.value());
    }

    @After("@annotation(ds)")
    public void clearDataSource(UseDataSource ds){
        RoutingDataSourceContextHolder.clearDataSourceType();
    }
}
